package game.util.template;

import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;

import org.apache.log4j.Logger;

public class Item {
	private static final Logger logger = Logger.getLogger(Item.class);

	public final static int TEXT = 1; // eg. <p>myText</p>

	public final static int TAG = 2; // eg. <!--#tagLabel#-->

	public final static int LOOP = 3; // eg. <!--#begin loopLabel#-->

	public final static int TAGLABEL = 0;

	public final static int TAGDEFAULT = 1;

	// make use of these by:
	// item.argv[Item.LOOPMAX]
	// item.argv[Item.LOOPDEFAULT] etc.
	public final static int LOOPLABEL = 1;

	public final static int LOOPMAX = 2;

	public final static int LOOPDEFAULT = 3;

	/*
	 * Item of type 3, LOOP, makes use of most argument.
	 * <!--#begin~loopLabel~record per page~default value#-->
	 * 
	 * Item of type 2, TAG, make use of 2 argument only. 
	 * <!--#tagLabel~default value#-->
	 */

	// peter: make it bigger, for ease of expansion
	public final static int MAX_NUM_ARGV = 10;

	public int type;

	public String[] argv = null;

	public String tag = null;

	public String str = null;

	public int max;

	public Template tem = null;

	public Item() {
		argv = new String[MAX_NUM_ARGV];
	}

	public int getType() {
		return type;
	}

	public String getTag() {
		return tag;
	}

	public String getString() {
		return str;
	}

	public void setType(int t) {
		this.type = t;
	}

	public void setTag(String tag) {
		this.tag = tag;
	}

	public void setString(String str) {
		this.str = str;
	}

	// peter: this comment is misleading
	// Handle plain text(type 1)
	public String toString() {
		// peter: changed
		return getString();
	}

	public void setTemplate(Template t) {
		this.tem = t;
	}

	/*
	 * Handle tag(type 2) return "myTag" from "<!--#myTag#-->"
	 */
	public String getTagLabel(String tag) {
		int s, e;

		s = tag.indexOf("#") + 1;
		e = tag.indexOf("#-->");
		return tag.substring(s, e);
	}

	// zenith: newly added on 9 Oct 2001
	// .... for getting maximum number of loops
	public static int getMaxLoop(String tag) {
		String delim1 = "#";
		String delim2 = "~";
		String tagContent;

		/*
		 * parse "tag argv" from "<!--#tag argv#-->
		 */
		StringTokenizer st = new StringTokenizer(tag, delim1);
		st.nextToken(); // discard "<!--"
		tagContent = st.nextToken();

		st = new StringTokenizer(tagContent, delim2);
		st.nextToken(); // discard "begin"
		st.nextToken(); // discard loop label
		String num = st.nextToken();

		try {
			return Integer.parseInt(num);
		} catch (Exception e) {
			return 0;
		}
	}

	/*
	 * peter: this kind of method, is better to be "static" Handle loop(type 3)
	 * return "myLabel" from "<!--#begin~myLabel~3~Default#-->"
	 */
	public static String getLoopLabel(String tag) {
		String delim1 = "#";
		String delim2 = "~";
		String tagContent;

		/*
		 * parse "tag argv" from "<!--#tag argv#-->
		 */
		StringTokenizer st = new StringTokenizer(tag, delim1);
		st.nextToken(); // discard "<!--"
		tagContent = st.nextToken();

		st = new StringTokenizer(tagContent, delim2);
		st.nextToken(); // discard "begin"

		return (st.nextToken());
	}

	/**
	 * parse the tag such that the arguments are stored in argv[] e.g. if a tag
	 * is like: <!--#begin~tag~argv2~argv3~argv4#--> then argv[0] = begin
	 * argv[1] = tag argv[2] = argv2 argv[3] = argv3 argv[4] = argv4
	 * 
	 */
	public void parse(String tag) {
		String delim1 = "#";
		String delim2 = "~";
		String tagContent;

		/*
		 * parse "tag argv" from "<!--#tag argv#-->
		 */
		StringTokenizer st = new StringTokenizer(tag, delim1);
		st.nextToken(); // discard "<!--"
		tagContent = st.nextToken();

		st = new StringTokenizer(tagContent, delim2);
		int i = 0;
		while (st.hasMoreTokens()) {
			argv[i++] = st.nextToken();

			// peter: safe guard to avoid
			// array index out-of-bound
			if (i >= MAX_NUM_ARGV)
				break;
		}
	}

	public Item duplicate() {
		Item newItem = new Item();

		newItem.type = this.type;
		newItem.tag = this.tag;
		newItem.str = this.str;

		for (int i = 0; i < this.argv.length; i++)
			newItem.argv[i] = this.argv[i];

		if (this.tem != null)
			newItem.tem = this.tem.duplicate();

		return newItem;
	}

	public static void main(String argv[]) {
		String loop = "<!--#begin~loopLabel~3~<p>Default Value</p>#-->";
		String tag = "<!--#tagName~Default Value#-->";
		Item item = new Item();

		item.parse(loop);

		Item test9 = new Item();
		test9.tag = "sub_Stupid";

		Item test1 = new Item();
		test1.tag = "Stupid";
		logger.info("test1.tag = " + test1.tag);
		// nested tem
		test1.tem = new Template();
		test1.tem.itemVector = new Vector();
		Item test2 = test1.duplicate();
		test2.tag = "Eric";
		logger.info("test2.tag = " + test2.tag);
		logger
				.info("test1.tag := "
						+ test1.tag
						+ " Pls note whether test1 is changed by alter the value of test2 ?");

		Item sub_item = null;
		Enumeration en = test1.tem.elements();
		while (en.hasMoreElements()) { // only one sub_item ==> test9
			sub_item = (Item) en.nextElement();
			logger.info("test1.tem.itemVector's sub_item = " + sub_item.tag);
		}

		Item sub_item2 = null;
		Enumeration en2 = test2.tem.elements();
		while (en2.hasMoreElements()) {
			sub_item2 = (Item) en2.nextElement();
			logger.info("test2.tem.itemVector's sub_item2 = " + sub_item2.tag);
			sub_item2.tag = "3rd Stupid";
			logger.info("test2.tem.itemVector's item = " + sub_item2.tag
					+ " value altered!!!");
		}
		logger.info("test1.tem.itemVector's item = " + sub_item.tag);
	}
}
